//
//  BBImagePreProcessor.m
//  BBTouch
//
//  Created by ben smith on 5/28/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBImagePreProcessor.h"
#import "BBMeshController.h"


@implementation BBImagePreProcessor

+(NSImage*)borderROI:(NSImage*)anImage
{
	NSBitmapImageRep * bitRep = [[anImage representations] objectAtIndex:0];
	[BBImagePreProcessor borderROI:bitRep roi:[[BBMeshController sharedMeshController] regionOfInterest]];
	return anImage;
}

+(void)borderROI:(NSBitmapImageRep*)bitRep roi:(NSRect)regionOfInterest
{
	// the default is for value = 0;
	[BBImagePreProcessor borderROI:bitRep roi:regionOfInterest colorValue:0];
}

// it is much quicker to cache your own ROI and call this method instead of the one above
+(void)borderROI:(NSBitmapImageRep*)bitRep roi:(NSRect)regionOfInterest colorValue:(unsigned char)value
{
  unsigned char *srcBytes = [bitRep bitmapData];
		
	int yMin = NSMinY(regionOfInterest);
	int yMax = NSMaxY(regionOfInterest);
	int xMax = NSMaxX(regionOfInterest);
	int xMin = NSMinX(regionOfInterest);
	
  int yIndex,xIndex;
	for (yIndex = yMin; yIndex < yMax; yIndex++) {
		srcBytes[(yIndex  * [bitRep bytesPerRow]) + (xMin * [bitRep samplesPerPixel])] = value; // x = min
		srcBytes[(yIndex  * [bitRep bytesPerRow]) + ((xMax - 1) * [bitRep samplesPerPixel])] = value; // x = max
	}
	for (xIndex = xMin; xIndex < xMax; xIndex++) {
		srcBytes[(yMin  * [bitRep bytesPerRow]) + (xIndex * [bitRep samplesPerPixel])] = value; // y = min
		srcBytes[((yMax - 1)  * [bitRep bytesPerRow]) + (xIndex * [bitRep samplesPerPixel])] = value; // y = max	
	}
}


@end
